import 'package:flutter/material.dart';

class ExpansionPanelDemo extends StatefulWidget {
  const ExpansionPanelDemo({Key? key}) : super(key: key);

  @override
  _ExpansionPanelDemoState createState() => _ExpansionPanelDemoState();
}

class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                SizedBox(
                  height: 15,
                ),
                SampleExpansionPanel(),
                SizedBox(
                  height: 15,
                ),
                SampleExpansionPanelList()
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class SampleExpansionPanel extends StatefulWidget {
  const SampleExpansionPanel({Key? key}) : super(key: key);

  @override
  _SampleExpansionPanelState createState() => _SampleExpansionPanelState();
}

class _SampleExpansionPanelState extends State<SampleExpansionPanel> {
  bool _isExpanded = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ExpansionPanelList(
        expansionCallback: (index, isExpanded) {
          setState(() {
            // 修改展开状态
            _isExpanded = !isExpanded;
          });
        },
        children: [
          ExpansionPanel(
              headerBuilder: (context, isExpanded) {
                return Container(
                  padding: EdgeInsets.all(16.0),
                  child: Text(
                    "京都风韵",
                    style: Theme.of(context).textTheme.title,
                  ),
                );
              },
              body: Container(
                padding: EdgeInsets.all(16.0),
                child: Text(
                  "北京的秋 浓墨重彩艳丽非凡~老舍曾将说过：“秋天一定要住在北平，北平的秋便是天堂”北平的秋绝对担得...",
                  style: Theme.of(context).textTheme.bodyText2,
                ),
              ),
              isExpanded: _isExpanded)
        ],
      ),
    );
  }
}

class SampleExpansionPanelList extends StatefulWidget {
  const SampleExpansionPanelList({Key? key}) : super(key: key);

  @override
  _SampleExpansionPanelListState createState() => _SampleExpansionPanelListState();
}

class ExpansionPaneItem {
  final String? headerText;
  final Widget? bodyWidget;
  bool isExpanded;

  ExpansionPaneItem({this.headerText, this.bodyWidget, this.isExpanded = false});
}

class _SampleExpansionPanelListState extends State<SampleExpansionPanelList> {
  List<ExpansionPaneItem> panelItemList = <ExpansionPaneItem>[];

  @override
  void initState() {
    super.initState();
    panelItemList.add(ExpansionPaneItem(
        headerText: "第一天", bodyWidget: buildBodyWidget("抵达成都，接站后入住酒店，我们是提前预定的所以小婉帮我们安排的宽窄巷子酒店（晚上逛吃逛吃超级方便哦，嘿嘿）")));
    panelItemList
        .add(ExpansionPaneItem(headerText: "第二天", bodyWidget: buildBodyWidget("成都出发，汶川遗址，5A级旅游景区：黄龙景区-九寨沟沟口")));
    panelItemList.add(ExpansionPaneItem(headerText: "第三天", bodyWidget: buildBodyWidget("全天游览九寨沟景区-晚上赠送藏羌歌舞晚会")));
    panelItemList
        .add(ExpansionPaneItem(headerText: "第四天", bodyWidget: buildBodyWidget("川主寺-特色藏寨-川主民族小镇-成都-游览琴台路，天府广场等")));
    panelItemList.add(ExpansionPaneItem(headerText: "第五天", bodyWidget: buildBodyWidget("深度游乐山-古栈道-凌云寺-乐山大佛")));
    panelItemList
        .add(ExpansionPaneItem(headerText: "第六天", bodyWidget: buildBodyWidget("峨眉山慢慢游：金顶，云海、佛光、圣灯，四面十方普贤，万年寺，清音阁")));
    panelItemList
        .add(ExpansionPaneItem(headerText: "第七天", bodyWidget: buildBodyWidget("著名的水利工程：都江堰（夫妻桥、二王庙）-熊猫基地，结束后送站")));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ExpansionPanelList(
        expansionCallback: (index, isExpanded) {
          panelItemList.forEach((element) {
            element.isExpanded = false;
          });
          setState(() {
            // 修改展开状态
            panelItemList[index].isExpanded = !isExpanded;
          });
        },
        children: panelItemList.map((e) {
          return ExpansionPanel(
              headerBuilder: (context, isExpand) {
                return Container(
                  padding: EdgeInsets.all(16.0),
                  child: Text(
                    e.headerText!,
                  ),
                );
              },
              body: e.bodyWidget!,
              isExpanded: e.isExpanded);
        }).toList(),
      ),
    );
  }

  buildBodyWidget(String bodyText) {
    return Container(
      padding: EdgeInsets.all(16.0),
      child: Text(
        bodyText,
      ),
    );
  }
}
